//
//  VideoDownLoadManager.m
//  JZMCFM
//
//  Created by Mc on 15/11/7.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "VideoDownLoadManager.h"
#import "VideoDownLoadOperation.h"
@implementation VideoDownLoadManager

+ (instancetype)sharedInstance
{
    static VideoDownLoadManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager = [[VideoDownLoadManager alloc] init];
            manager.httpOperationDict = [NSMutableDictionary dictionary];
            manager.downVideoArray = [NSMutableArray array];
        }
    });
    return manager;
}

- (void)startAVideoWithVideoModel:(DetailModel2 *)downLoadVideo
{
    if (!self.downLoadQueue) {
        self.downLoadQueue = [[NSOperationQueue alloc] init];
        self.downLoadQueue.maxConcurrentOperationCount = 3;
    }
    VideoDownLoadOperation *ope = [[VideoDownLoadOperation alloc] initWithDownLoadVideoModel:downLoadVideo];
    ope.updateBlock = ^(DetailModel2 *model){
        if (self.delegate && [self.delegate respondsToSelector:@selector(videoDidUpdatedProgressWithVideoModel:)]) {
            [self.delegate videoDidUpdatedProgressWithVideoModel:model];
        }
    };
    
    [self.httpOperationDict setObject:ope forKey:downLoadVideo.playUrl64];
    [self.downLoadQueue addOperation:ope];
    [self.downVideoArray addObject:downLoadVideo];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:k_newVideoDidStartDown object:nil];
    
}

- (void)downloadPausewithModel:(DetailModel2 *)pauseModel
{
    VideoDownLoadOperation *ope = [self.httpOperationDict objectForKey:pauseModel.playUrl64];
    if (!ope.isFinished) {
        [ope downLoadPause];
    }
}

- (void)downloadResumeWithModel:(DetailModel2 *)resumeModel
{
    VideoDownLoadOperation *ope = [self.httpOperationDict objectForKey:resumeModel.playUrl64];
    if (!ope.isFinished) {
        [ope downLoadResume];
    }
}

@end
